package com.yj.ccs.abs.web.asset.controller;

import com.yj.ccs.abs.boot.authorization.annotation.CurrentUser;
import com.yj.ccs.abs.boot.pagination.Page;
import com.yj.ccs.abs.common.dto.IdDto;
import com.yj.ccs.abs.common.model.BaseResult;
import com.yj.ccs.abs.web.asset.dto.AssetListDto;
import com.yj.ccs.abs.web.asset.dto.ContractDetailDto;
import com.yj.ccs.abs.web.asset.dto.UpdateAssetStatusDto;
import com.yj.ccs.abs.web.asset.entity.*;
import com.yj.ccs.abs.web.asset.service.AssetService;
import com.yj.ccs.abs.web.management.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/21
 * Author     : Unbelievable9
 **/
@Api(tags = "Web-Asset-Controller")
@RestController
@RequestMapping("/web/asset")
public class AssetController {

    private final AssetService assetService;

    @Autowired
    public AssetController(AssetService assetService) {
        this.assetService = assetService;
    }

    @ApiOperation("获取资产列表")
    @GetMapping("/list")
    public ResponseEntity<BaseResult> waitToApplyList(@Validated AssetListDto assetListDto, @CurrentUser User user) {
        Page<AssetPageInfo> assetPageInfo = assetService.getAssetPage(assetListDto);
        return new ResponseEntity<>(BaseResult.success("获取资产列表成功").setData(assetPageInfo), HttpStatus.OK);
    }

    @ApiOperation("修改资产状态")
    @PostMapping("/change-status")
    public ResponseEntity<BaseResult> changeStatus(@Validated @RequestBody UpdateAssetStatusDto updateAssetStatusDto, @CurrentUser User user) {
        String errorMessage = assetService.updateAssetStatus(updateAssetStatusDto, user);

        if (errorMessage == null) {
            return new ResponseEntity<>(BaseResult.success("修改资产状态成功"), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(BaseResult.error(errorMessage), HttpStatus.OK);
        }
    }

    @ApiOperation("获取资产详情顶部信息")
    @GetMapping("/top-info")
    public ResponseEntity<BaseResult> topInfo(@Validated IdDto idDto, @CurrentUser User user) {
        Asset asset = assetService.getAssetById(idDto.getId());

        if (asset == null) {
            return new ResponseEntity<>(BaseResult.error("资产信息不存在"), HttpStatus.OK);
        } else {
            AssetTopInfo assetTopInfo = assetService.getAssetTopInfo(asset);
            return new ResponseEntity<>(BaseResult.success("获取资产详情基础信息成功").setData(assetTopInfo), HttpStatus.OK);
        }
    }

    @ApiOperation("获取资产详情基础信息")
    @GetMapping("/basic-info")
    public ResponseEntity<BaseResult> basicInfo(@Validated IdDto idDto, @CurrentUser User user) {
        Asset asset = assetService.getAssetById(idDto.getId());

        if (asset == null) {
            return new ResponseEntity<>(BaseResult.error("资产信息不存在"), HttpStatus.OK);
        } else {
            AssetBasicInfo assetBasicInfo = assetService.getAssetBasicInfo(asset);

            return new ResponseEntity<>(BaseResult.success("获取资产详情基础信息成功").setData(assetBasicInfo), HttpStatus.OK);
        }
    }

    @ApiOperation("获取资产详情合同信息")
    @GetMapping("/contract-detail")
    public ResponseEntity<BaseResult> upstreamContractInfo(@Validated ContractDetailDto contractDetailDto, @CurrentUser User user) {
        Asset asset = assetService.getAssetById(contractDetailDto.getId());

        if (asset == null) {
            return new ResponseEntity<>(BaseResult.error("资产信息不存在"), HttpStatus.OK);
        } else {
            ContractDetail contractDetail = assetService.getContractDetailByAssetIdAndType(asset.getId(), contractDetailDto.getContract_type());

            if (contractDetail == null) {
                return new ResponseEntity<>(BaseResult.error("合同信息不存在"), HttpStatus.OK);
            } else {
                return new ResponseEntity<>(BaseResult.success("获取资产详情合同信息成功").setData(contractDetail), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("获取资产详情货权信息")
    @GetMapping("/property-ownership-detail")
    public ResponseEntity<BaseResult> propertyOwnershipDetail(@Validated IdDto idDto, @CurrentUser User user) {
        Asset asset = assetService.getAssetById(idDto.getId());

        if (asset == null) {
            return new ResponseEntity<>(BaseResult.error("资产信息不存在"), HttpStatus.OK);
        } else {
            PropertyOwnershipDetail propertyOwnershipDetail = assetService.getPropertyOwnershipDetail(asset.getId());

            if (propertyOwnershipDetail == null) {
                return new ResponseEntity<>(BaseResult.error("货权信息不存在"), HttpStatus.OK);
            } else {
                return new ResponseEntity<>(BaseResult.success("获取资产详情合同信息成功").setData(propertyOwnershipDetail), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("获取发票信息列表")
    @GetMapping("/receipt-list")
    public ResponseEntity<BaseResult> receiptList(@Validated IdDto idDto, @CurrentUser User user) {
        Asset asset = assetService.getAssetById(idDto.getId());

        if (asset == null) {
            return new ResponseEntity<>(BaseResult.error("资产信息不存在"), HttpStatus.OK);
        } else {
            ReceiptListInfo receiptListInfo = assetService.getReceiptListInfo(asset.getId());

            return new ResponseEntity<>(BaseResult.success("获取发票信息列表成功").setData(receiptListInfo), HttpStatus.OK);
        }
    }

    @ApiOperation("获取结算信息列表")
    @GetMapping("/settle-account-list")
    public ResponseEntity<BaseResult> settleAccountList(@Validated IdDto idDto, @CurrentUser User user) {
        Asset asset = assetService.getAssetById(idDto.getId());

        if (asset == null) {
            return new ResponseEntity<>(BaseResult.error("资产信息不存在"), HttpStatus.OK);
        } else {
            SettleAccountListInfo settleAccountListInfo = assetService.getSettleAccountListInfo(idDto.getId());

            return new ResponseEntity<>(BaseResult.success("获取结算信息列表成功").setData(settleAccountListInfo), HttpStatus.OK);
        }
    }

    @ApiOperation("获取回款信息列表")
    @GetMapping("/fund-restream-list")
    public ResponseEntity<BaseResult> fundRestreamList(@Validated IdDto idDto, @CurrentUser User user) {
        Asset asset = assetService.getAssetById(idDto.getId());

        if (asset == null) {
            return new ResponseEntity<>(BaseResult.error("资产信息不存在"), HttpStatus.OK);
        } else {
            FundRestreamListInfo fundRestreamListInfo = assetService.getFundRestreamListInfo(idDto.getId());

            return new ResponseEntity<>(BaseResult.success("获取回款信息列表成功").setData(fundRestreamListInfo), HttpStatus.OK);
        }
    }

    @ApiOperation("获取备注信息列表")
    @GetMapping("/remark-list")
    public ResponseEntity<BaseResult> remarkList(@Validated IdDto idDto, @CurrentUser User user) {
        Asset asset = assetService.getAssetById(idDto.getId());

        if (asset == null) {
            return new ResponseEntity<>(BaseResult.error("资产信息不存在"), HttpStatus.OK);
        } else {
            RemarkListInfo remarkListInfo = assetService.getRemarkListInfo(idDto.getId());

            return new ResponseEntity<>(BaseResult.success("获取发票信息列表成功").setData(remarkListInfo), HttpStatus.OK);
        }
    }
}
